namespace SubhadraSolutions.Sharp.Logging.LogPublishers
{
    using SubhadraSolutions.Sharp.Logging.Contracts;
    using SubhadraSolutions.Sharp.Utils.Diagnostics;
    using SubhadraSolutions.Sharp.Utils.Diagnostics.Performance;

    public sealed class ExecutionFlowLogPublisher : AbstractBufferedLogPublisher
    {
        #region Fields

        private static readonly ExecutionFlowLogPublisher _instance =
            new ExecutionFlowLogPublisher();

        #endregion Fields

        #region Constructors

        private ExecutionFlowLogPublisher()
        {
            Enabled = true;
        }

        #endregion Constructors

        #region Properties

        public static ExecutionFlowLogPublisher Instance
        {
            get { return _instance; }
        }

        public bool Enabled
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        protected override void initializeProtected()
        {
            base.initializeProtected();
            if (Enabled)
            {
                ExecutionFlowRecorder.Instance.OnExecutionFlowRecorded +=
                    Instance_OnExecutionFlowRecorded;
            }
        }

        private void Instance_OnExecutionFlowRecorded(object sender,
            ExecutionFlowRecordedEventArgs e)
        {
            var item =
                new LogStoreItem(
                    e.ExecutionFlow.Exception == null ? LogItemSeverity.DEBUG : LogItemSeverity.ERROR, LogItemCategories.Technical,
                    e.ExecutionFlow);
            LogStore.Write(item);
        }

        #endregion Methods

        #region Other

        //protected override ILogStore getLogStore()
        //{
        //    return new ImmediateLogStore();
        //}

        #endregion Other
    }
}